using System.Web.Services;
using System.Xml;
using System;

namespace umlungu.engineX.FtpX {
	/// <summary>
	/// umlungu Gatekeeper. 
	/// Validation of users for access to engineX internet-based services is managed from within the Gatekeeper.
	/// </summary>
	[WebService(Namespace="http://www.umlungu.com/engineX/")]
	public class ftpX : x_result {
		#region Private methods
		#endregion

		#region Protected methods
		/// <summary>Constructor</summary>
		public ftpX() : base("ftpX") {
		}
		#endregion

		#region General web service methods
		
		/// <summary>
		/// Gets the files from a remote server via ftp
		/// </summary>
		[WebMethod] public XmlDocument LIST(string remote_host, string remote_user, string remote_password, string remote_path, string remote_mask) {
			remote_path = (remote_path != "")? remote_path : ".";
			remote_mask = (remote_mask != "")? remote_mask : "*";
			x_ftp ff = new x_ftp(remote_host, remote_path, remote_user, remote_password, 21, true);
			try {
				ff.login();
				ff.chdir(remote_path);
				string[] fileNames = ff.FileList(remote_mask);
				_AddOk();
				for(int i=0;i < fileNames.Length;i++) {
					_Add("file", fileNames[i]);
				}
				_Add("debug", ff.Message);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			if (ff != null)
				ff.close();
			return(Result);
		}
		
		/// <summary>
		/// Gets the files from a remote server via ftp
		/// </summary>
		[WebMethod] public XmlDocument Upload(string remote_host, string remote_user, string remote_password, string remote_path, string filename) {
			remote_path = (remote_path != "")? remote_path : ".";
			x_ftp ff = new x_ftp(remote_host, remote_path, remote_user, remote_password, 21, true);
			try {
				ff.login();
				ff.chdir(remote_path);
				ff.upload(filename);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			if (ff != null)
				ff.close();
			return(Result);
		}
		#endregion

	}
}
